package cat.sun.dialogs;

import java.util.List;

import cat.sun.R;
import cat.sun.stats.Score;
import cat.sun.stats.Stats;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class WinDialog extends DialogFragment
{
	private Stats stats;
	private long time;
	private EditText txtName;
	private String savedText = "";
	
	public static WinDialog newInstance(long time)
	{
		WinDialog dialog = new WinDialog();		
		Bundle bundle = new Bundle();
		bundle.putLong("TIME", time);
		dialog.setArguments(bundle);
		return dialog;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{		
		super.onCreate(savedInstanceState);
		setStyle(DialogFragment.STYLE_NO_FRAME, 0);	
		setCancelable(false);		
		this.stats = new Stats(getActivity().getSharedPreferences(getActivity().getIntent().getExtras().getString("Table"), 0),getActivity().getIntent().getExtras().getString("Table"),getResources());		
		this.time = getArguments().getLong("TIME");
		if (savedInstanceState != null && savedInstanceState.getString("NAME") != null) this.savedText = savedInstanceState.getString("NAME");
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
	{
		View view = inflater.inflate(R.layout.dialog_win, container,false);		
		final Context context = getActivity();	
	        	    				
		final ImageView close = (ImageView) view.findViewById(R.id.dialog_win_close);
		final EditText text = (EditText) view.findViewById(R.id.dialog_win_edit);
		final Button ok = (Button) view.findViewById(R.id.dialog_win_ok);
		TextView title = (TextView) view.findViewById(R.id.dialog_win_title);
		
		this.txtName = text;
		this.txtName.setText(savedText);		
				
		text.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		ok.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		title.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Medium.ttf"));
		
		close.setOnClickListener(new View.OnClickListener() 
		{			
			@Override
			public void onClick(View v) 
			{
				String name = text.getText().toString();
				if (name.length() == 0)
				{					
					name = context.getResources().getString(R.string.Player);
				}				
				List<Score> score = stats.GetBestScores();
				score = stats.AddToList(score, new Score(name,(float)(time)/1000));
				stats.AddToStats(score);
				dismiss();
			}			
		});		

		ok.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v) 
			{
				String name = text.getText().toString();				
				if (name.length() == 0)
				{					
					name = context.getResources().getString(R.string.Player);
				}				
				List<Score> score = stats.GetBestScores();
				score = stats.AddToList(score, new Score(name,(float)(time)/1000));
				stats.AddToStats(score);	
				dismiss();
			}			
		});
		
		getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() 
    	{			
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) 
			{
				if (keyCode == KeyEvent.KEYCODE_BACK) 
				{
					String name = text.getText().toString();
					if (name.length() == 0)
					{					
						name = context.getResources().getString(R.string.Player);
					}				
					List<Score> score = stats.GetBestScores();
					score = stats.AddToList(score, new Score(name,(float)(time)/1000));
					stats.AddToStats(score);	
					dismiss();
                }
				return true;
			}
		});
		
		return view;
	}
	
	@Override
	public void onSaveInstanceState(Bundle arg0) 
	{
		arg0.putString("NAME", this.txtName.getText().toString());
	}
}
